﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

using Game.Terrain;

namespace Game.Editor.Toolbox
{
    public class TerrainItemControl2 : UserControl
    {
        readonly Canvas _Canvas = new Canvas();
        readonly Button _Button = new Button();
        readonly Image _Image = new Image();

        Game.Terrain.TerrainType _TerrainType;

        public TerrainItemControl2()
        {
            InitializeComponent();

            SelectedTerrainType.Selected += delegate
            {
                this.UpdatePressedState();
            };
        }

        void InitializeComponent()
        {
            _Canvas.Width = double.NaN;
            _Canvas.Height = double.NaN;
            _Canvas.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            _Canvas.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            Grid.SetColumn(_Canvas, 0);
            Grid.SetRow(_Canvas, 0);
            Grid grid = new Grid();
            this.Content = grid;
            grid.Children.Add(_Canvas);

            _Button.Width = 50;
            _Button.Height = 50;
            _Button.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            _Button.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            Canvas.SetLeft(_Button, 0);
            Canvas.SetTop(_Button, 0);
            _Button.Click += Button_Click;
            _Canvas.Children.Add(_Button);

            _Image.Width = double.NaN;
            _Image.Height = double.NaN;
            _Image.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            _Image.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            _Button.Content = _Image;
        }

        public Brush ToolColor
        {
            get { return _Button.Background; }
            set { _Button.Background = value; }
        }

        public TerrainType TerrainType
        {
            get { return _TerrainType; }
            set { _TerrainType = value; DisplayType(); }
        }

        void DisplayType()
        {
            if (_TerrainType != null)
            {
                _Image.Source = _TerrainType.ImageSource;

                ToolTip t = new ToolTip();
                t.Content = "" + _TerrainType.Name;
                ToolTipService.SetToolTip(_Button, t);
            }
        }

        void Button_Click(object sender, RoutedEventArgs e)
        {
            SelectedTerrainType.TerrainType = _TerrainType;
        }

        void UpdatePressedState()
        {
            if (SelectedTerrainType.TerrainType == this._TerrainType)
            {
                _Button.Background = new SolidColorBrush(SystemColors.HighlightColor);
            }
            else
            {
                _Button.Background = new SolidColorBrush(SystemColors.ControlLightColor);
            }
        }

    }
}
